package com.hanbang.chu_xing_gao_zhao.myview;


import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hanbang.chu_xing_gao_zhao.utils.MyLog;
import com.hanbang.chu_xing_gao_zhao.utils.SystemTools;
import com.hanbang.cxgz_2.R;


/**
 * 购物车加减号 图标
 *
 * @author Administrator
 */
public class NumberSelectView extends LinearLayout {
    private OnItemClickListener onItemClickListener = null;
    private int textSize = 30;
    private int dimenSize = 30;
    private int lineSize = 1;
    private int lineColor = 0xffcccccc;
    private int centreColor = 0xFFFFFFFF;
    private int buttonTextColor = 0xff777777;
    private int centreTextColor = 0xFF000000;
    private TextView numTextView = null;
    private ImageView addImageView = null;
    private ImageView subImageView = null;
    private int number = 1;
    private final int maxNumber = 100;
    private final int minNumber = 1;
    private boolean isAuto = false;

    public boolean isAuto() {
        return isAuto;
    }

    public void setAuto(boolean isAuto) {
        this.isAuto = isAuto;
    }

    public NumberSelectView(Context context) {
        super(context, null);
    }

    public NumberSelectView(Context context, AttributeSet attrs) {
        super(context, attrs);
        TypedArray a = context.obtainStyledAttributes(attrs,
                R.styleable.NumberSelectView);
        textSize = a.getDimensionPixelSize(
                R.styleable.NumberSelectView_textSize, textSize);
        dimenSize = a.getDimensionPixelSize(
                R.styleable.NumberSelectView_dimenSize, dimenSize);
        MyLog.e("dimenSize", dimenSize + "");
        lineSize = a.getDimensionPixelSize(
                R.styleable.NumberSelectView_lineSize, lineSize);
        centreColor = a.getColor(R.styleable.NumberSelectView_centreColor,
                centreColor);
        buttonTextColor = a.getColor(
                R.styleable.NumberSelectView_buttonTextColor, buttonTextColor);
        centreTextColor = a.getColor(
                R.styleable.NumberSelectView_centreTextColor, centreTextColor);
        lineColor = a.getColor(R.styleable.NumberSelectView_lineColor,
                lineColor);

        a.recycle();

        initView();
        setNumber(number);
    }

    private void initView() {
        LayoutParams params = new LayoutParams(dimenSize, dimenSize);
        LayoutParams params0 = new LayoutParams(LayoutParams.WRAP_CONTENT,
                LayoutParams.MATCH_PARENT);
        params0.weight = 1;
        params0.gravity = Gravity.CENTER;
        params.gravity = Gravity.CENTER;
        LayoutParams params2 = new LayoutParams(lineSize,
                LayoutParams.MATCH_PARENT);
        params2.gravity = Gravity.CENTER;

        subImageView = new ImageView(getContext());
        subImageView.setOnClickListener(onClickListener);
        subImageView.setImageResource(R.drawable.jian_hao);
        subImageView.setPadding(dimenSize / 3, dimenSize / 3, dimenSize / 3,
                dimenSize / 3);
        SystemTools.setImageViewTint(subImageView, R.color.black);
        addView(subImageView, params);

        View view = new View(getContext());
        view.setBackgroundColor(lineColor);
        addView(view, params2);

        numTextView = new TextView(getContext());
        numTextView.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
        numTextView.setTextColor(centreTextColor);
        numTextView.setText(String.valueOf(number));
        numTextView.setPadding(dimenSize / 2, 0, dimenSize / 2, 0);
        numTextView.setBackgroundColor(centreColor);
        numTextView.setGravity(Gravity.CENTER);
        params0.setMargins(0, lineSize, 0, lineSize);
        addView(numTextView, params0);

        View view2 = new View(getContext());
        view2.setBackgroundColor(lineColor);
        addView(view2, params2);

        addImageView = new ImageView(getContext());
        addImageView.setOnClickListener(onClickListener);
        addImageView.setImageResource(R.drawable.jia_hao);
        addImageView.setPadding(dimenSize / 3, dimenSize / 3, dimenSize / 3,
                dimenSize / 3);
        SystemTools.setImageViewTint(subImageView, R.color.black);
        addView(addImageView, params);
    }

    private OnClickListener onClickListener = new OnClickListener() {

        @Override
        public void onClick(View v) {
            if (v == addImageView) {

                if (onItemClickListener != null) {
                    onItemClickListener.onAddClick(number,
                            NumberSelectView.this);
                }
            } else if (v == subImageView) {
                if (onItemClickListener != null) {
                    onItemClickListener.onSubClick(number,
                            NumberSelectView.this);
                }
                if (isAuto) {
                    setNumber(number - 1);
                }
            }
        }
    };

    public void setNumber(int num) {
        if (numTextView != null && num >= minNumber && num <= maxNumber) {
            numTextView.setText(String.valueOf(num));
            number = num;
            if (number == minNumber) {
                addImageView.setEnabled(true);
                subImageView.setEnabled(false);
                SystemTools.setImageViewTint(subImageView, R.color.gray_e8);
                SystemTools.setImageViewTint(addImageView, R.color.black);

            } else if (number == maxNumber) {
                addImageView.setEnabled(false);
                subImageView.setEnabled(true);
                SystemTools.setImageViewTint(subImageView, R.color.black);
                SystemTools.setImageViewTint(addImageView, R.color.gray_e8);
            } else {
                addImageView.setEnabled(true);
                subImageView.setEnabled(true);
                SystemTools.setImageViewTint(subImageView, R.color.black);
                SystemTools.setImageViewTint(addImageView, R.color.black);
            }
        }
    }

    public int getNumber() {
        return number;
    }

    public int getTextSize() {
        return textSize;
    }

    public void setTextSize(int textSize) {
        this.textSize = textSize;
        invalidate();
    }

    public int getDimenSize() {
        return dimenSize;
    }

    public void setDimenSize(int dimenSize) {
        this.dimenSize = dimenSize;
        invalidate();
    }

    public int getLineSize() {
        return lineSize;
    }

    public void setLineSize(int lineSize) {
        this.lineSize = lineSize;
        invalidate();
    }

    public int getCentreColor() {
        return centreColor;
    }

    public void setCentreColor(int centreColor) {
        this.centreColor = centreColor;
        invalidate();
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    /*
     * 点击的回调接口
     */
    public interface OnItemClickListener {
        void onAddClick(int num, NumberSelectView view);

        void onSubClick(int num, NumberSelectView view);
    }
}
